<template>
	<uni-nav-bar title="整点打卡" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
	<view class="package_sale">
		<view>
			<view>拍照</view>
			<mk-upload :imgList="imgList" @onDelete="onDelete" @onChoose="onChoose" :maxCount="1" :sourceType="sourceType"/>
		</view>
		<view class="top">
			<view>备注</view>
			<textarea class="textarea" v-model="value"></textarea>
		</view>
		<view class="but" @click="submit()">确定打卡</view>
	</view>
	<Watermark ref="watermark" @selected="selected"></Watermark>
</template>

<script setup lang="ts">
	import { navBack, navTo } from '@/utils/navigator';
	import { ref } from 'vue';
	import Watermark from '@c/watermark.vue';
	import { errorToast, showLoading, successToast } from '@/utils/prompt';
	import { toPublish } from '@mqtt';
	import { onLoad } from '@dcloudio/uni-app';
	import { getDriverID } from '@/stores/driverID';
	const staff_id = getDriverID();
	const banID = ref()
	const sourceType = ["camera"]
	onLoad((pearm) => {
		banID.value = Number(pearm.banId)
	})
	// 上传图片
	const imgList = ref([])
	const image = ref()
	const watermark = ref(null)
	const value = ref('');
	// 上传图片
	function onChoose(e : any) {
		watermark.value.fillTextToImg(e.tempFilePaths[0])
		console.log(e.tempFilePaths[0])
	}
	function selected(e : any) {
		uni.uploadFile({
			url: 'https://admin.anlan.tech/api/common/upload2',
			filePath: e,
			name: 'file',
			formData: {
				'user': 'test'  // 上传附带参数
			},
			success: (uploadFileRes) => {
				// 根据接口具体返回格式   赋值具体对应url
				imgList.value.push(JSON.parse(uploadFileRes.data).data.fullurl)
				image.value = JSON.parse(uploadFileRes.data).data.url
			},
			fail: (err : any) => {
				console.log('err', err)
			}
		});
	}
	// 删除图片
	function onDelete() {
		imgList.value = []
		image.value = ''
	}
	//整点打卡
	function submit() {
		if (!image.value) {
			errorToast('请上传图片')
			return
		}
		showLoading()
		const payload = {
			staff_id,
			ban_id: banID.value,
			image: image.value,
			remarks: value.value,
		};
		toPublish('staff/addBanClock',
			payload,
			(obj : any) => {
				uni.hideLoading();
				const { code, msg } = obj;
				if (code === 1) {
					successToast(msg)
					setTimeout(() => {
						navBack()
					}, 1000)
				} else {
					errorToast(msg)
				}
			}
		);
	}
</script>

<style scoped lang="less">
	.top {
		margin-top: 20rpx;
	}

	.textarea {
		background-color: #eee;
		border-radius: 5px;
		margin-top: 20rpx;
		padding: 20rpx;
		box-sizing: border-box;
		height: 150rpx;
	}

	.but {
		width: 360rpx;
		height: 98rpx;
		background: #558AF1;
		border-radius: 5px;
		color: #fff;
		text-align: center;
		line-height: 98rpx;
		margin: 80rpx auto 30rpx;
	}
</style>
<style>
	.top>>>.checklist-box {
		margin-right: 0px;
	}

	.top>>>.uni-data-checklist {
		flex: none;
	}

	.package_sale>>>.mk-upload {
		padding: 0px;
	}

	.top>>>.textarea {
		width: 100%;
	}
</style>